using System;
using System.Collections;
using System.IO;
using System.Text;

namespace WowAceUpdater
{
    internal static class ChangeLog
    {
        #region Fields

        public static ArrayList Addons = new ArrayList();

        private static readonly Object oLock = new Object();

        #endregion

        #region Public Methods

        public static void AddAddon(string Name, VersionNumber Old, VersionNumber New)
        {
            AddonInfo Addon;
            Addon.Name = Name;
            Addon.OldVersion = Old;
            Addon.NewVersion = New;

            lock (oLock)
            {
                Addons.Add(Addon);
            }
        }

        public static void ClearAddons()
        {
            lock (oLock)
            {
                Addons.Clear();
            }
        }

        public static StringBuilder CreateChangeLog()
        {
            try
            {
                lock (oLock)
                {
                    return CreateChangeLog_L();
                }
            }
            catch (Exception e)
            {
                Program.LogDbg(e.ToString());
            }

            return null;
        }

        public static string[] GetChangeLogInfo(AddonInfo Addon)
        {
            String changeLogFileName =
                Path.Combine(WowEnvironment.GetAddonDirectory(Addon.Name),
                             String.Format("Changelog-{1}-r{0}.txt", Addon.NewVersion, Addon.Name));
            if (!File.Exists(changeLogFileName))
            {
                return null;
            }

            String TXTContent = File.ReadAllText(changeLogFileName);
            TXTContent = TXTContent.Replace("\r", "\n");
            TXTContent = TXTContent.Replace("\n\n", "\r");
            if (TXTContent.IndexOf("r" + string.Format("{0}", Addon.OldVersion.Major)) >= 0)
            {
                return
                    TXTContent.Substring(0, TXTContent.IndexOf("r" + string.Format("{0}", Addon.OldVersion.Major)) - 1).
                        Split('\r');
            }
            else
            {
                return TXTContent.Split('\r');
            }
        }

        #endregion

        #region Private Methods

        private static StringBuilder CreateChangeLog_L()
        {
            StringBuilder HTML =
                new StringBuilder(
                    "<html><head></head><body><h1><center>Changelog</center></h1><p /><p />%Content%</body></html>");
            String AddonHeaderLog = "<hr /><h3>%AddonName% - Changes from %OldRev% to %NewRev%</h3>";
            String AddonFooter = "<p />";
            String HTMLBody = "";
            String Temp;
            String TempContainer = "";

            foreach (AddonInfo Addon in Addons)
            {
                if (Addon.NewVersion.Major > Addon.OldVersion.Major)
                {
                    Temp = AddonHeaderLog.Replace("%AddonName%", Addon.Name);
                    Temp = Temp.Replace("%OldRev%", string.Format("{0}", Addon.OldVersion.Major));
                    Temp = Temp.Replace("%NewRev%", string.Format("{0}", Addon.NewVersion.Major));
                    HTMLBody = HTMLBody + Temp;
                    Temp = "";
                    String AddonChangeLog = "";

                    String[] Changes = GetChangeLogInfo(Addon);
                    if (Changes == null) continue;
                    if (Changes[0].IndexOf("<?xml version=") >= 0) continue;

                    foreach (String Change in Changes)
                    {
                        if ((Change.Trim('-').Trim() == "") || (Change == "" || Change.Substring(0, 3) == "   ") ||
                            (Change == "Changed paths:"))
                            continue;

                        if (Change.IndexOf(" | ") >= 0)
                        {
                            uint I;
                            if (Change[0] == 'r' &&
                                UInt32.TryParse(Change.Substring(1, Change.IndexOf("|") - 2).Trim(), out I))
                            {
                                if (TempContainer != "")
                                {
                                    AddonChangeLog = TempContainer.Replace("%Changes%", Temp) + AddonChangeLog;
                                    Temp = "";
                                }
                                TempContainer = "<ul><li>" + Change + "</li>%Changes%</ul>";
                                continue;
                            }
                        }
                        Temp = Temp + "<li>" + Change + "</li>";
                    }
                    AddonChangeLog = TempContainer.Replace("%Changes%", Temp) + AddonChangeLog + AddonFooter;
                    HTMLBody = HTMLBody + AddonChangeLog;
                    TempContainer = "";
                }
            }
            HTML = HTML.Replace("%Content%", HTMLBody);

            return HTML;
            //File.WriteAllText(fileName, HTML);
        }

        #endregion
    }

    internal struct AddonInfo
    {
        public string Name;
        public VersionNumber NewVersion;
        public VersionNumber OldVersion;
    }
}